﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace homework12
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void buttonFileMerge_Click(object sender, EventArgs e)
        {
            // 选择第一个文件
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.Filter = "文本文件 (*.txt)|*.txt";
            openFileDialog1.Title = "选择第一个文件";

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string file1Path = openFileDialog1.FileName;

                // 选择第二个文件
                OpenFileDialog openFileDialog2 = new OpenFileDialog();
                openFileDialog2.Filter = "文本文件 (*.txt)|*.txt";
                openFileDialog2.Title = "选择第二个文件";

                if (openFileDialog2.ShowDialog() == DialogResult.OK)
                {
                    string file2Path = openFileDialog2.FileName;

                    try
                    {
                        // 读取第一个文件的内容
                        string file1Content = File.ReadAllText(file1Path);
                        //打开需要合并的第一个文件
                        System.Diagnostics.Process.Start(file1Path);
                        // 读取第二个文件的内容
                        string file2Content = File.ReadAllText(file2Path);
                        //打开合并的第二个文件
                        System.Diagnostics.Process.Start(file2Path);
                        // 合并内容
                        string mergedContent = "第一个文件中的内容："+file1Content + Environment.NewLine + "第二个文件中的内容："+file2Content;

                        // 创建新文件夹
                        string directoryPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Data");
                        Directory.CreateDirectory(directoryPath);

                        // 写入合并后的内容到新文件
                        string mergedFilePath = Path.Combine(directoryPath, "merged_file.txt");
                        File.WriteAllText(mergedFilePath, mergedContent);

                        MessageBox.Show("文件合并成功并保存到 " + mergedFilePath, "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("发生错误: " + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
        }

        private void buttonOpenMergedFile_Click(object sender, EventArgs e)
        {
            // 打开合并后的文件
            string directoryPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Data");
            string mergedFilePath = Path.Combine(directoryPath, "merged_file.txt");

            try
            {
                if (File.Exists(mergedFilePath))
                {
                    System.Diagnostics.Process.Start(mergedFilePath);
                }
                else
                {
                    MessageBox.Show("合并后的文件不存在！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("无法打开文件: " + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
